﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SQLite;


namespace HomeWork11
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        static SQLiteConnection  m_dbConnection;
        static int count = 0;

        private static SQLiteConnection dbConnection()
        {
            SQLiteConnectionStringBuilder connectionString = new SQLiteConnectionStringBuilder();
            connectionString.DataSource = @"dictionary.db";

            m_dbConnection = new SQLiteConnection(connectionString.ToString());
            m_dbConnection.Open();

            return m_dbConnection;
        }

        public static void close()
        {
            try
            {
                if (m_dbConnection.State == ConnectionState.Open)
                    m_dbConnection.Close();
                else if (m_dbConnection.State == ConnectionState.Broken)
                {
                    m_dbConnection.Close();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("closeErr:" + ex);
            }
        }

        public static string sqlone(string sql)
        {

            try
            {
                SQLiteCommand sqlcmd = new SQLiteCommand(sql, dbConnection());//sql语句
                return sqlcmd.ExecuteScalar().ToString();
            }
            catch
            {
                return "";
            }
            finally
            {
                close();
            }
        }


        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter) {
                string sql = "select en from character where ch='" + Chinese.Text + "'";
                string answer = sqlone(sql);

                if (answer == textBox.Text)
                {
                    this.success.Visible = true;
                    this.next.Visible = true;
                }
                else
                {
                    this.wrong.Visible = true;
                    this.next.Visible = true;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.success.Visible = false;
            this.wrong.Visible = false;
            this.next.Visible = false;
            this.textBox.Text = "";
            count++;
            count = count % 4;
            string sql = "select ch from character where id=" + count;
            this.textBox.Text = sqlone(sql);
        }
    }
}
